/**
 * Created by Ado on 15/9/15.
 */
class GuideController extends egret.HashObject{
    public stepConfig:GuideStep[];

    public maxStep:number;
    private curStep:GuideStep;
    private curMiniStep:Step;
    private petDisplay:eui.Image;
    //displays of the guide
    private infoBoardBg:egret.Bitmap;
    private infoBoard:egret.Bitmap;
    private maskShape0:egret.Shape;
    private maskShape1:egret.Shape;
    private maskShape2:egret.Shape;
    private maskShape3:egret.Shape;
    private finger:egret.Bitmap;

    //need enterframe?
    public name:string;
    private frameItem:FrameItem;

    public constructor(){
        super();
        this.name = "guidecontroller";
        this.stepConfig = [];
        this.maskShape0 = new egret.Shape();
        this.maskShape0.touchEnabled = true;
        this.maskShape1 = new egret.Shape();
        this.maskShape1.touchEnabled = true;
        this.maskShape2 = new egret.Shape();
        this.maskShape2.touchEnabled = true;
        this.maskShape3 = new egret.Shape();
        this.maskShape3.touchEnabled = true;
        this.infoBoard = new egret.Bitmap();
        this.infoBoard.touchEnabled = false;
        //this.infoBoard.addEventListener(eui.UIEvent.CONTENT_CHANGED, this.onTextChanged, this);
        this.infoBoardBg = new egret.Bitmap(RES.getRes("info_bg"));
        this.infoBoardBg.touchEnabled = false;
        this.infoBoardBg.cacheAsBitmap = true;
        AnchorUtil.setAnchor(this.infoBoardBg,0,1);

        this.finger = new egret.Bitmap();
        this.finger.touchEnabled = false;
        this.frameItem = new FrameItem();
        this.frameItem.callFun = this.update;
        this.frameItem.callObj = this;
        Model.GuideCall = this.nextStep;
        Model.GuideObserver = this;
    }

    private onResourceLoadComplete(event:RES.ResourceEvent):void {
        window["__showConnection"](false);
        if (event.groupName == "guides") {
            this.updateDisplay();
        }
    }

    public start(){
        //return;
        if( this.stepConfig.length == 0){
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            window["__showConnection"](true);
            RES.loadGroup("guides");
            this.parseSteps();
        }
        else
            return;
        FrameMgr.getInstance().addControll(this.frameItem);
        if( this.curStep == null){
            this.curStep = this.stepConfig[Model.me.step];
            this.nextStep();
        }
       //if(! RES.isGroupLoaded("guides")){
       //    RES.loadGroup("guides");
       //    RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE,this.onResLoaded, this);
       //}
        this.updateDisplay();
        Model.GuideStarted = true;
    }
    //private resLoaded:boolean = false;
    //private onResLoaded(evt:RES.ResourceEvent){
    //    if( evt.groupName == "guides"){
    //        this.resLoaded = true;
    //        if( this.curMiniStep != null){
    //            this.updateDisplay();
    //        }
    //    }
    //}
    private referGap:number = 1000/6;
    private curGap:number=0;
    //private textGap:number = 3000;
    //private curTextGap:number = 0;
    public update(delta:number){
        this.curGap+=delta;
        if( this.finger.parent != null){
            if( this.curMiniStep.action == 0 || this.curMiniStep.action == -1 || this.curMiniStep.action == 2){
                if( this.curGap > this.referGap){
                    this.curframe++;
                    if( this.curframe >= this.animmax){
                        this.curframe = this.curMiniStep.action == 2 ? 3 : 0;
                    }
                    this.finger.texture = RES.getRes("tap"+this.curframe);
                    this.curGap = 0;
                }
            }else{
                this.finger.x -= 3;
                if( this.finger.x < this.curMiniStep.rect.left - 20 ){
                    this.finger.x = this.curMiniStep.rect.right - 30;
                }
            }
        }

        //this.curTextGap+=delta;
        //
        //if( this.infoBoard.parent != null && this.curTextGap >= this.textGap && this.clickright){
        //    this.curTextGap = 0;
        //    this.infoBoard.source = this.curMiniStep.getText();
        //}
    }

    //private onTextChanged(evt:eui.UIEvent){
    //    if( this.infoBoard != null && this.infoBoard.texture != null ){
    //        this.infoBoard.x = this.infoBoardBg.x + (this.infoBoardBg.width - this.infoBoard.texture.textureWidth)*0.5 + 10;
    //        this.infoBoard.y = this.infoBoardBg.y - this.infoBoardBg.height + (this.infoBoardBg.height - this.infoBoard.texture.textureHeight)*0.5;
    //    }
    //}

    public parseSteps(){
        var guideList:any[] = GuideConfig.guides;
        var len:number = guideList.length;
        for(var idx = 0; idx < len; idx++){
            this.stepConfig.push(new GuideStep(guideList[idx]));
        }
        this.maxStep = this.stepConfig[idx-1].id;
    }

    public nextStep(evt:egret.TouchEvent=null){
        this.cleanStep();
        if( this.curMiniStep != null && this.curMiniStep.send){
            window["__guide"](this.curStep.id);
        }
        if( this.curStep.curStepId < this.curStep.maxStep){
            this.curMiniStep = this.curStep.nextStep();
            //if( this.resLoaded)
                this.updateDisplay();
        }else if(this.curStep.id < this.maxStep){
            //window["__guide"](this.curStep.id);
            this.curStep = this.stepConfig[this.curStep.id ];
            this.curMiniStep = this.curStep.nextStep();
            //if( this.resLoaded)
                this.updateDisplay();
        }else{
            //window["__guide"](this.maxStep);
            this.theEnd();
        }
    }

    public cleanStep(){
        if( this.finger != null && this.finger.parent != null){
            this.finger.parent.removeChild(this.finger);
        }
        if( this.infoBoard != null && this.infoBoard.parent != null){
            this.infoBoard.parent.removeChild(this.infoBoard);
        }
        if( this.infoBoardBg != null && this.infoBoardBg.parent != null){
            this.infoBoardBg.parent.removeChild(this.infoBoardBg);
        }
        if( this.maskShape0 != null && this.maskShape0.parent != null){
            this.maskShape0.parent.removeChild(this.maskShape0);
        }
        if( this.maskShape1 != null && this.maskShape1.parent != null){
            this.maskShape1.parent.removeChild(this.maskShape1);
        }
        if( this.maskShape2 != null && this.maskShape2.parent != null){
            this.maskShape2.parent.removeChild(this.maskShape2);
        }
        if( this.maskShape3 != null && this.maskShape3.parent != null){
            this.maskShape3.parent.removeChild(this.maskShape3);
        }
        if( this.petDisplay != null && this.petDisplay.parent != null){
            this.petDisplay.parent.removeChild(this.petDisplay);
        }
        //this.clickright = true;
        GameConst.Stage.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.nextStep, this);
        GameConst.Stage.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.checkTarget, this);
        GameConst.Stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.checkTarget, this);
    }
    //private clickright:boolean = true;
    private checkTarget(evt:egret.TouchEvent){
        var target:egret.DisplayObject= evt.target;
        //this.clickright = (target.name == this.curMiniStep.bindName) || (target.name != null && target.name.indexOf(this.curMiniStep.bindName) != -1) || this.curMiniStep.bindName == "";
        //if( !this.clickright){
        //    while(target.parent != null){
        //        target = target.parent;
        //        this.clickright =  (target.name == this.curMiniStep.bindName) || (target.name != null && target.name.indexOf(this.curMiniStep.bindName) != -1) || this.curMiniStep.bindName == "";
        //        if( this.clickright) break;
        //    }
        //}
        //
        //if( !this.clickright ){
        //    //console.log(this.curMiniStep.bindName," not found");
        //    this.infoBoard.source = this.curMiniStep.errorText;
        //}else{
            this.infoBoard.texture = RES.getRes(this.curMiniStep.getText());
        if( this.infoBoard.texture){
            this.infoBoard.x = this.infoBoardBg.x + (this.infoBoardBg.width - this.infoBoard.texture.textureWidth)*0.5;
            this.infoBoard.y = this.infoBoardBg.y - this.infoBoardBg.height + (this.infoBoardBg.height - this.infoBoard.texture.textureHeight)*0.5;
        }

        //}
    }
    private animmax:number;
    private curframe:number;
    private updateDisplay(){
        if( this.curMiniStep != null) {
            if( this.curMiniStep.action == -2) {
                Model.GuideType = this.curMiniStep.guidetype;
                Model.GuideNeedValue = this.curMiniStep.guidevalue;
                this.cleanStep();
                return;
            }
            if (this.curMiniStep.action != -1) {
                Model.GuideType = this.curMiniStep.guidetype;
                Model.GuideNeedValue = this.curMiniStep.guidevalue;
                if( this.curMiniStep.action == 0 || this.curMiniStep.action == 2){
                    GameConst.Stage.addEventListener(egret.TouchEvent.TOUCH_TAP, this.checkTarget, this);
                }else if(this.curMiniStep.action == 1){
                    GameConst.Stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.checkTarget, this);
                }
            } else {
                GameConst.Stage.addEventListener(egret.TouchEvent.TOUCH_TAP, this.nextStep, this);
            }
            if (!this.curMiniStep.needUpdateDisplay) return;
            this.updatePetDisplay();
            if( !this.infoBoardBg.texture){
                this.infoBoardBg.texture = RES.getRes("info_bg");
                AnchorUtil.setAnchor(this.infoBoardBg,0,1);
            }
            if( SceneMgr.getInstance().currentScene == SceneMgr.SCENE_MAIN){
                this.infoBoardBg.x = 240;
                this.infoBoardBg.y = 220+this.infoBoardBg.height;
            }else{
                this.infoBoardBg.x = this.curMiniStep.infox;
                this.infoBoardBg.y = this.curMiniStep.infoy+this.infoBoardBg.height;
            }
            if( this.curMiniStep.texts && this.curMiniStep.texts.length != 0){

                GameConst.Stage.addChild(this.infoBoardBg);
                if( PerformanceMonitor.getInstance().getSwithState(PerformanceMonitor.EFFECTS)){
                    this.afterTween();
                }else{
                    this.infoBoardBg.scaleX = this.infoBoardBg.scaleY = 0;
                    var tw:egret.Tween = egret.Tween.get(this.infoBoardBg);
                    tw.to({scaleX: 1, scaleY: 1}, 300).call(this.afterTween, this);
                }
            }else{
                this.afterTween();
            }
        }
    }

    private afterTween(){
        var rect:egret.Rectangle = this.curMiniStep.rect;
        var g:egret.Graphics = this.maskShape0.graphics;
        g.clear();
        g.beginFill(0,0);
        g.drawRect(0,0,rect.x+rect.width,rect.y);
        g.endFill();
        g = this.maskShape1.graphics;
        g.clear();
        g.beginFill(0,0);
        g.drawRect(rect.x+rect.width,0,GameConst.StageW-rect.x-rect.width,rect.y+rect.height);
        g.endFill();
        g = this.maskShape2.graphics;
        g.clear();
        g.beginFill(0,0);
        g.drawRect(rect.x,rect.y+rect.height,GameConst.StageW-rect.x,GameConst.StageH-rect.y-rect.height);
        g.endFill();
        g = this.maskShape3.graphics;
        g.clear();
        g.beginFill(0,0);
        g.drawRect(0,rect.y,rect.x,GameConst.StageH-rect.y);
        g.endFill();

        GameConst.Stage.addChild(this.maskShape0);
        GameConst.Stage.addChild(this.maskShape1);
        GameConst.Stage.addChild(this.maskShape2);
        GameConst.Stage.addChild(this.maskShape3);

        //if( this.curMiniStep.text.length != 0){
        if( this.curMiniStep.texts && this.curMiniStep.texts.length != 0){
            this.infoBoard.texture = RES.getRes(this.curMiniStep.texts[0]);
            if( this.infoBoard.texture){
                this.infoBoard.x = this.infoBoardBg.x + (this.infoBoardBg.width - this.infoBoard.texture.textureWidth)*0.5;
                this.infoBoard.y = this.infoBoardBg.y - this.infoBoardBg.height + (this.infoBoardBg.height - this.infoBoard.texture.textureHeight)*0.5;
            }
            //this.infoBoard.x = this.infoBoardBg.x+10;
            //this.infoBoard.y = this.infoBoardBg.y - this.infoBoardBg.height + 40;
            GameConst.Stage.addChild(this.infoBoard);
        }

        //}
        this.updateFingerDisplay();
    }

    private updateFingerDisplay(){
        if( this.curMiniStep.action != -1){
            if( this.curMiniStep.action == 0){//touch
                this.animmax = 3;
                this.curframe = 0;
            }else if( this.curMiniStep.action == 1){//touch move
                this.animmax = 1;
                this.curframe = 0;
            }else if( this.curMiniStep.action == 2){//touch traverse
                this.animmax = 5;
                this.curframe = 3;
            }
            if( this.curMiniStep.action == 1){
                this.finger.texture = RES.getRes("tap1");
                this.finger.x = this.curMiniStep.rect.right+10;
                this.finger.y = this.curMiniStep.rect.y+this.curMiniStep.rect.height*0.5 - 15;
            }else if( this.curMiniStep.action == 0){
                this.finger.texture = RES.getRes("tap1");
                this.finger.x = this.curMiniStep.rect.x+this.curMiniStep.rect.width*0.5 - 15;
                this.finger.y = this.curMiniStep.rect.y+this.curMiniStep.rect.height*0.5 - 15;
            }else if( this.curMiniStep.action == 2){
                this.finger.texture = RES.getRes("tap3");
                this.finger.x = this.curMiniStep.rect.x+(this.curMiniStep.rect.width-this.finger.texture.textureWidth)*0.5;
                this.finger.y = this.curMiniStep.rect.y - this.finger.texture.textureHeight*0.5;
            }
        }
        else{
            this.animmax = 2;
            this.curframe = 0;
            this.finger.x = this.infoBoard.x + 70;
            this.finger.y = this.infoBoard.y + 50;
        }
        GameConst.Stage.addChild(this.finger);
    }

    private updatePetDisplay(){
        if( this.curMiniStep.needPet){
            if( SceneMgr.getInstance().currentScene != SceneMgr.SCENE_MAIN){
                if( this.petDisplay == null){
                    this.petDisplay = new eui.Image();
                    this.petDisplay.source = "resource/assets/petpreview/"+PetMgr.getInstance().curPet.petId+".png";
                    this.petDisplay.width = 96;
                    this.petDisplay.height = 110;
                    this.petDisplay.x = 5;
                    this.petDisplay.y = 585;
                }
                GameConst.Stage.addChild(this.petDisplay);
            }
        }
    }

    public theEnd(){
        this.destroy();
    }

    public destroy(evt:any=null){
        FrameMgr.getInstance().delController(this.name);
        GameConst.Stage.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.nextStep, this);
        //RES.destroyRes("guides");
        var step:GuideStep;
        while( this.stepConfig.length){
            step = this.stepConfig.pop();
            step.destroy();
            step = null;
        }
        Model.GuideType = null;
        Model.GuideNeedValue = null;
        Model.GuideCall = null;
        Model.GuideObserver = null;
        Model.GuideStarted = false;
        this.stepConfig = null;
        this.curStep = null;
        this.curMiniStep = null;
        this.finger = null;
        this.infoBoard = null;
        this.maskShape0.graphics.clear();
        this.maskShape0 = null;
        this.maskShape1.graphics.clear();
        this.maskShape1 = null;
        this.maskShape2.graphics.clear();
        this.maskShape2 = null;
        this.maskShape3.graphics.clear();
        this.maskShape3 = null;
        this.infoBoardBg = null;
        Model.GuideStarted = false;
        GuideController._instance = null;
    }

    private static _instance:GuideController;
    public static getInstance():GuideController{
        if( GuideController._instance == null)
            GuideController._instance = new GuideController();
        return GuideController._instance;
    }
}